#include "TRR.h"

namespace Cxuh{

TRR::TRR(MergingSegment* ms1, int e1): core(ms1), radius(e1){
	if(!ms1 || e1 < 0)
		throw(" Error: TRR::TRR(): Generating illegal TRR!");

	Point<int> ptl = core->get_left_point();
	Point<int> ptr = core->get_right_point();
	int xl = ptl.getX();
	int yl = ptl.getY();
	int xr = ptr.getX();
	int yr = ptr.getY();
	int z = ptl.getZ();

	left.setXYZ(xl-e1, yl, z);
	right.setXYZ(xr+e1, yr, z);

	switch (core->get_slope()) {
	case 0:
		// a sink
		top.setXYZ(xl, yl + e1, z);
		bottom.setXYZ(xl, yl - e1, z);
		break;
	case 1:
		top.setXYZ(xr, yr + e1, z);
		bottom.setXYZ(xl, yl - e1, z);
		break;
	case -1:
		top.setXYZ(xl, yl + e1, z);
		bottom.setXYZ(xr, yr - e1, z);
		break;
	default:
		throw(" Error: TRR::TRR(): unknown slope of core!");
	}

	e_tl.set_points(top, left);
	e_tr.set_points(top, right);
	e_bl.set_points(bottom, left);
	e_br.set_points(bottom, right);
}

TRR::~TRR(void){
}

Edge TRR::merge(const TRR& trr2) const{
	// merge two TRRs
	Edge* temp_e = NULL;

	temp_e = e_tl.merge(trr2.e_br);
	if(!temp_e)
		temp_e = e_tr.merge(trr2.e_bl);
	if(!temp_e)
		temp_e = e_bl.merge(trr2.e_tr);
	if(!temp_e)
		temp_e = e_br.merge(trr2.e_tl);

	if(!temp_e)
		throw(" Error: TRR::merge(): no overlap between two TRRs!");
	else
		return *temp_e;
}

}
